package com.example.home_stretch;
import android.graphics.Bitmap;
import android.util.Log;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.EncodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import java.util.HashMap;
import java.util.Map;
import static androidx.constraintlayout.widget.Constraints.TAG;
public class QR_code_one {
    private int WHITE = 0xFFFFFFFF;
    private  int BLACK = 0xFF000000;
    public Bitmap Zxing(String content){
        //创建键值对来储存生成二维码需要的额外参数
        Map<EncodeHintType,Object> map = new HashMap<>();
        //二维码的字符集
        map.put(EncodeHintType.CHARACTER_SET,"utf-8");
        //二维码的边距
        map.put(EncodeHintType.MARGIN,2);
        try {
            //生成二维码，（参数：编码内容，编码方式，首选宽度，首选高度，额外参数）
            BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,300,300,map);
            int W = encode.getWidth();      //二维码的宽度
            int H = encode.getHeight();     //二维码的高度
            int[] pixel = new int[W*H];     //二维码的平方
            //二维码的位图参数，（参数为：二维码宽度，二维码高度，图片的配置）
            Bitmap bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
            for (int i = 0; i < H; i++) {
                for (int j = 0; j < W; j++) {
                    //根据二维矩阵数据创建数组
                    int offset = i * W;
                    pixel[offset + j] = encode.get(j,i) ? BLACK : WHITE ;
                }
            }
            //设置位图像素集为数组
            bitmap.setPixels(pixel,0,W,0,0,W,H);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String Scanning(String tvScanning, Bitmap zxing){
        //获取图片
        Bitmap obmp = zxing;
        //获取图片的宽度
        int width = obmp.getWidth();
        //获取图片的高度
        int height = obmp.getHeight();
        //获取图片的平方
        int[] data = new int[width * height];
        //获取位图像素集数组
        obmp.getPixels(data, 0, width, 0, 0, width, height);
        //使用RGBLuminanceSource来解析二维码，获取（宽度，高度，平方）
        RGBLuminanceSource source = new RGBLuminanceSource(width, height, data);
        //二进制位图BinaryBitmap解析二维码
        BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
        //二维码读取器
        QRCodeReader reader = new QRCodeReader();
        try {
            //返回的结果存入result
            Result result = reader.decode(bitmap);
            //使用TextView进行输出
            tvScanning = result.toString();
            Log.i(TAG, "inin: "+result.toString());

        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (ChecksumException e) {
            e.printStackTrace();
        } catch (FormatException e) {
            e.printStackTrace();
        }
        return tvScanning;
    }
}
